package com.solcoo.merchant.ui.order;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.common.App;
import com.android.common.util.DateUtil;
import com.solcoo.merchant.R;
import com.solcoo.merchant.base.BaseListAdapter;
import com.solcoo.merchant.model.Account;
import com.solcoo.merchant.model.Order;

public class OrderMemberAdapter extends BaseListAdapter<Order> {

	public OrderMemberAdapter(Context context) {
		super(context);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if (convertView != null) {
			holder = (ViewHolder) convertView
					.getTag(R.layout.order_member_item);
		} else {
			holder = new ViewHolder();
			convertView = LayoutInflater.from(context).inflate(
					R.layout.order_member_item, null);
			holder.orderDate = (TextView) convertView.findViewById(R.id.date);
			holder.orderIcon = (ImageView) convertView
					.findViewById(R.id.oder_icon);
			holder.ordernName = (TextView) convertView
					.findViewById(R.id.order_name);
			holder.orderValue = (TextView) convertView
					.findViewById(R.id.order_value);
			convertView.setTag(R.layout.order_member_item, holder);
		}
		Order order = getItem(position);
		if (position == 0
				|| !DateUtil.isSameDay(order.dingdan.shijian,
						getItem(position - 1).dingdan.shijian)) {
			holder.orderDate.setText(DateUtil.format(order.dingdan.shijian,
					DateUtil.DATE_ZH));
			holder.orderDate.setVisibility(View.VISIBLE);
		} else {
			holder.orderDate.setVisibility(View.GONE);
		}
		if (order.dingdan.leixing == Account.PAY) {
			holder.ordernName.setText("支付");
			holder.orderIcon.setImageResource(R.drawable.pay_icon);
			holder.orderValue.setTextColor(App.getResources().getColor(
					R.color.pay_color));
			holder.orderValue.setText("-" + String.valueOf(order.dingdan.jine));
		} else {
			holder.ordernName.setText("充值");
			holder.orderIcon.setImageResource(R.drawable.recharge_icon);
			holder.orderValue.setTextColor(App.getResources().getColor(
					R.color.recharge_color));
			holder.orderValue.setText("+" + String.valueOf(order.dingdan.jine));
		}
		return convertView;
	}

	private static class ViewHolder {
		TextView orderDate;
		ImageView orderIcon;
		TextView ordernName;
		TextView orderValue;
	}
}
